package ncu.me.musicplayer.player;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import ncu.me.musicplayer.entity.MusicInfo;
import ncu.me.musicplayer.sql.MusicDao;

/**
 * @Author ye
 * @Date 2020/6/2 12:29
 * @Version 1.0
 */
public class MusicManager {
    public static MusicManager getInstance(){
        return Holder.INSTANCE;
    }
    static class Holder{
        private static MusicManager INSTANCE = new MusicManager();
    }

    //单曲播放
    public static final int SINGLE_PLAY=1;
    //循环播放
    public static final int LOOP_PLAY=2;
    //随机播放
    public static final int RAND_PLAY=3;
    private int mPlatType = LOOP_PLAY;

    //是否开始
    private boolean isStart;
    //是否在播放
    private boolean isPlaying;

    //歌曲列表播放项发生改变监听集合
    Map<Object,IndexChangedListener> mIndexListeners = new HashMap<>();
    //歌曲播放状态发生改变监听集合
    Map<Object,PlayStateChangedListener> mStateListeners = new HashMap<>();
    IPlayer mPlayer;
    List<MusicInfo> mList = new ArrayList<>();
    int mIndex = -1;

    //初始化
    public void init(IPlayer player){
        mPlayer = player;
        mList = MusicDao.getInstance().getAll();
        mPlayer.setOnFinishedListener(() -> {
            if (!isStart){
                return;
            }
            switch (mPlatType){
                case SINGLE_PLAY:
                    play(mIndex);
                    break;
                case LOOP_PLAY:
                    next();
                    break;
                case RAND_PLAY:
                    int rand = -1;
                    for (int i = 0; i < 3; i++) {
                        rand = new Random().nextInt(mList.size());
                        if (rand != mIndex){
                            break;
                        }
                    }
                    play(rand);
                    break;
                default:
            }
        });
    }
    private IPlayer player(){
        return mPlayer;
    }
    public List<MusicInfo> getList(){
        return mList;
    }
    public void play(int index){
        setIndex(index);
        play();
    }
    public void play(){

        if (mList.size()==0){
            return;
        }
        isStart = true;
        setPlaying(true);
        if (mIndex < 0 ){
            setIndex(0);
        }
        String filePath = mList.get(mIndex).getFilePath();
        if (filePath!=null&&!filePath.equals("")){
            File file = new File(filePath);
            if (file.exists()){
                mPlayer.play(filePath);
                return;
            }
        }
    }
    public void next(){
        int index = mIndex + 1;
        if (index>=mList.size()){
            setIndex(0);
        }else {
            setIndex(index,true);
        }
        play();
    }
    public void previous(){
        int index = mIndex - 1;
        if (index<0){
            setIndex(mList.size()-1);
        }else {
            setIndex(index,true);
        }
        play();
    }
    public void pause(){
        mPlayer.pause();
        setPlaying(false);
    }
    public void resume(){
        //当没有开始播放时，直接开始播放
        if (!isStart){
            play();
            return;
        }
        mPlayer.resume();
        setPlaying(true);
    }
    public void setPlaying(boolean isPlaying){
        if (isPlaying==this.isPlaying){
            return;
        }
        this.isPlaying = isPlaying;
        //向注册了该事件的监听者分发事件
        mStateListeners.forEach((o, listener) -> listener.onChange(this.isPlaying));
    }

    public boolean isPlaying() {
        return isPlaying;
    }

    public int size(){
        return mPlayer.size();
    }
    public int pos(){
        return mPlayer.pos();
    }
    public void seek(int progress) {
        mPlayer.seek(progress);
    }
    public void add(MusicInfo info, boolean isPlay){
        if (!getList().contains(info)){
            mList.add(0,info);
            MusicDao.getInstance().insert(info);
            setIndex(0,true);
        }else {
            setIndex(getList().indexOf(info));
        }
        if (isPlay){
            play();
        }
    }

    public void setIndex(int index){
        setIndex(index,false);
    }


    /**
     *
     * @param index index与mIndex相通时不处理
     * @param changed 为true一定分发事件
     */
    private void setIndex(int index,boolean changed){
        if (index < 0 || index >= mList.size()){
            return;
        }
        if (index != mIndex || changed){
            mIndex = index;
            //向注册了该事件的监听者分发事件
            mIndexListeners.forEach((o, listener) -> listener.onChange(index));
        }
    }
    public MusicInfo get(){
        if (mIndex >= 0 && mIndex < mList.size()){
            return mList.get(mIndex);
        }else {
            return null;
        }
    }
    public int getIndex(){return mIndex;}
    public boolean isStart(){return isStart;}
    public void addIndexChangedListener(Object target,IndexChangedListener listener){
        mIndexListeners.put(target,listener);
    }
    public void removeIndexChangedListener(Object target){
        mIndexListeners.remove(target);
    }
    public void addStateChangedListener(Object target,PlayStateChangedListener listener){
        mStateListeners.put(target,listener);
    }
    public void removeStateChangedListener(Object target){
        mStateListeners.remove(target);
    }

    public void setPlayType(int playType){
        mPlatType = playType;
    }
    public int getPlayType() {
        return mPlatType;
    }

    /**
     * 播放列表项发生改变
     */
    public interface IndexChangedListener{
        void onChange(int index);
    }

    /**
     * 播放状态发生改变
     */
    public interface PlayStateChangedListener{
        void onChange(boolean isPlaying);
    }
}
